from machine import Pin,PWM,UART             
import utime
import gc
import ujson
import network
from machine import SPI
import st7789
import urequests
spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8))

display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT))
display.init()
display.fill(st7789.color565(0,0,0))
def connectWIFI():  #网络连接
    print('Connecting wifi...')
    sta_wlan = network.WLAN(network.STA_IF)
    sta_wlan.active(True)
    sta_wlan.connect('OPPO K9s 5G', '888888888', security=network.AUTH_PSK)
    while(sta_wlan.config() == '0.0.0.0'):
        utime.sleep(1)
    print('Connected...')
def shangyu(m):  #上传于云端
    url = "http://post.blackwalnut.zucc.edu.cn/thingswise/idos/jets/streams/dev/32101205/DemonstrationCase___R503.R503_input_json"
    headers = {
        "content-type": "application/json",
    }

    formData = {
        'data':{
            "case": "Identification",
            "aht20": "instance1"
        },
        'auth':{
            'user': 'OPPO K9s 5G',
            'password': '888888888'
        }
    }


    try:
        formData['data']['Identification'] = m


        res = urequests.post(url, data=ujson.dumps(formData),
                        headers=headers)

        print(res.status_code)
                
        print(res.text)

    except Exception as err:
        print(err)
    gc.collect()
    utime.sleep(1)
connectWIFI()
uart1=UART(1,baudrate=57600,parity=None,tx=Pin(0),rx=Pin(1),timeout=10) 
pin13 = Pin(13)
pin13.init(Pin.IN)
pin13.pull(Pin.PULL_DOWN)
print(pin13.value())
# uart1.write(bytes([0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X08,0X32,0X03,0x01,0X02,0x01,0x01,0x00,0x43]))
# utime.sleep_ms(2000)
print(pin13.value())
pin11 = Pin(11)
pin11.init(Pin.IN)
pin11.pull(Pin.PULL_DOWN)
print(pin11.value())
if pin11.value()==1:  #按键处于高电平
    uart1.write(bytes([0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X08,0X31,0X01,0x01,0X01,0x01,0x01,0x00,0x3f]))#发送识别指纹指令包
    b=uart1.read()
    display.draw_string(10, 90, 'Please enter ', color=st7789.WHITE, bg=st7789.WHITE, size=4, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
    display.draw_string(10, 130, 'your fingerprint', color=st7789.WHITE, bg=st7789.WHITE, size=4, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
    utime.sleep_ms(2000)
    sdisplay.fill(st7789.color565(0,0,0))
else:  #按键处低电平
    while 1:
        uart1.write(bytes([0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X08,0X32,0X03,0x01,0X02,0x01,0x01,0x00,0x43]))  #发送识别指纹指令包 
        utime.sleep_ms(2000)
        b=uart1.read(47)  #读取缓冲区47位
        print(b)
        if b==b'\xef\x01\xff\xff\xff\xff\x07\x00\x08\x00\x01\x00\x00\x00\x00\x00\x10\xef\x01\xff\xff\xff\xff\x07\x00\x08\x00\x02\x00\x00\x00\x00\x00\x11\xef\x01\xff\xff\xff\xff\x07\x00\x08\x00\x03\x00\x01':
            display.draw_string(10, 90, 'Recognition', color=st7789.WHITE, bg=st7789.WHITE, size=4, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
            display.draw_string(10, 130, 'Successful!', color=st7789.WHITE, bg=st7789.WHITE, size=4, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
            utime.sleep_ms(2000)
            display.fill(st7789.color565(0,0,0))
            utime.sleep_ms(10000)  #判断应答包是否正确，正确则等待10s让人进入
        print(pin13.value())
        updata='0'
        shangyu(updata)  #数据上传云端
        while pin13.value()==1:   #有人闯入触发报警并上传云端
            p = PWM(3, Pin(12))
            p.freq(1048)
            p.duty(50) 
            utime.sleep(0.1) 
            p.duty(0)
            utime.sleep_ms(500)
            p = PWM(3, Pin(12))
            p.freq(1048)
            p.duty(50) 
            utime.sleep(0.1)
            p.duty(0)
            utime.sleep_ms(500) 
            p = PWM(3, Pin(12))
            p.freq(1048)
            p.duty(50) 
            utime.sleep(0.1)
            p.duty(0)
            utime.sleep_ms(500)
            updata='1'
            shangyu(updata)